# coding=utf-8
import json

import requests
import xlrd
import xlwt
from amqp.five import string

from bs4 import BeautifulSoup
from lxml import etree


def test_data():
    workbook = xlwt.Workbook()
    m = 0
    sheet1 = workbook.add_sheet('data')
    url = "	https://data.sh.gov.cn/interface/2520/7351"
    header = {"content-type": "application/json", "token": "3151f827171a62b5f4ec799b5d065d66"}
    cityInfo = {"黄浦": ["外滩街道", "南京东路街道", "半淞园路街道", "小东门街道", "老西门街道", "豫园街道", "打浦桥街道", "淮海中路街道", "瑞金二路街道", "五里桥街道"],
                "长宁": ["华阳路街道", "新华路街道", "江苏路街道", "天山路街道", "周家桥街道", "虹桥街道", "仙霞新村街道", "程家桥街道", "北新泾街道", "新泾镇"],
                "徐汇": ["湖南路街道", "天平路街道", "枫林路街道", "徐家汇街道", "斜土路街道", "长桥街道", "漕河泾街道", "康健新村街道", "虹梅路街道", "田林街道",
                        "凌云路街道", "龙华街道", "华泾镇"],
                "静安": ["江宁路街道", "静安寺街道", "南京西路街道", "曹家渡街道", "石门二路街道", "天目西路街道", "北站街道", "宝山路街道", "芷江西路街道", "共和新路街道",
                        "大宁路街道", "彭浦新村街道", "临汾路街道", "彭浦镇"],
                "普陀": ["长寿路街道", "曹杨新村街道", "长风新村街道", "宜川路街道", "甘泉路街道", "石泉路街道", "真如镇街道", "万里街道", "长征镇", "桃浦镇"],
                "虹口": ["四川北路街道", "北外滩街道", "欧阳路街道", "广中路街道", "凉城新村街道", "嘉兴路街道", "曲阳路街道", "江湾镇街道"],
                "杨浦": ["定海路街道", "大桥街道", "平凉路街道", "江浦路街道", "控江路街道", "殷行街道", "长白新村街道", "延吉新村街道", "五角场街道", "四平路街道",
                        "新江湾城街道", "长海路街道"],
                "闵行": ["江川路街道", "古美路街道", "新虹街道", "浦锦街道", "莘庄镇", "七宝镇", "浦江镇", "梅陇镇", "虹桥镇", "马桥镇", "吴泾镇", "华漕镇",
                        "颛桥镇"],
                "宝山": ["吴淞街道", "张庙街道", "友谊路街道", "庙行镇", "罗店镇", "大场镇", "顾村镇", "罗泾镇", "杨行镇", "月浦镇", "淞南镇", "高境镇"],
                "嘉定": ["嘉定镇街道", "新成路街道", "真新街道", "马陆镇", "南翔镇", "江桥镇", "安亭镇", "外冈镇", "徐行镇", "华亭镇", ],
                "浦东": ["潍坊新村街道", "陆家嘴街道", "塘桥街道", "周家渡街道", "东明路街道", "洋泾街道", "上钢新村街道", "沪东新村街道", "金杨新村街道", "浦兴路街道",
                         "南码头路街道", "花木街道", "川沙新镇", "合庆镇", "曹路镇", "高东镇", "高桥镇", "高行镇", "金桥镇", "张江镇", "唐镇", "北蔡镇", "三林镇",
                         "惠南镇", "新场镇", "大团镇", "周浦镇", "航头镇", "康桥镇", "宣桥镇", "祝桥镇", "泥城镇", "书院镇", "万祥镇", "老港镇", "南汇新城镇", ],
                "金山": ["石化街道", "枫泾镇", "朱泾镇", "亭林镇", "漕泾镇", "山阳镇", "金山卫镇", "张堰镇", "廊下镇", "吕巷镇", ],
                "松江": ["岳阳街道", "中山街道", "永丰街道", "方松街道", "九里亭街道", "广富林街道", "九亭镇", "泗泾镇", "泖港镇", "车墩镇", "洞泾镇", "叶榭镇",
                        "新桥镇", "石湖荡镇", "新浜镇", "佘山镇", "小昆山镇", ],
                "青浦": ["夏阳街道", "盈浦街道", "香花桥街道", "赵巷镇", "徐泾镇", "华新镇", "重固镇", "白鹤镇", "朱家角镇", "练塘镇", "金泽镇", ],
                "奉贤": ["西渡街道", "奉浦街道", "金海街道", "南桥镇", "庄行镇", "金汇镇", "柘林镇", "青村镇", "奉城镇", "四团镇", "海湾镇", ],
                "崇明": ["城桥镇", "堡镇", "庙镇", "中兴镇", "新河镇", "三星镇", "向化镇", "绿华镇", "建设镇", "陈家镇", "竖新镇", "港西镇", "港沿镇", "新海镇",
                        "东平镇", "长兴镇", "新村乡", "横沙乡"]}
    for district in cityInfo:
        for town in cityInfo[district]:
            print(district,town)
            body = {"district": str(district), "town": str(town), "limit": 1000}
            print(body)
            r1 = requests.post(url, headers=header, json=body)
            result = r1.json()["data"]
            print("当前接口返回的数据是："+str(result))
            j = json.loads(result)
            print(j["data"])
            if int(j["total"]) > 0:
                for i in j["data"]:
                    communityname = i["communityname"]
                    district = i["district"]
                    town = i["town"]
                    sheet1.write(m, 0, communityname)
                    sheet1.write(m, 1, district)
                    sheet1.write(m, 2, town)
                    m += 1
    workbook.save('shanghai.xls')


def loupan_Info():
    url = "	https://data.sh.gov.cn/interface/2520/7351"
    header = {"content-type": "application/json", "token": "3151f827171a62b5f4ec799b5d065d66"}


    body = {"district": "黄浦", "town": "外滩街道", "limit": 1000}

    r1 = requests.post(url, headers=header, json=body)
    result = r1.json()["data"]
    print(result)

if __name__ == '__main__':
    # test_data()
    loupan_Info()
